//
//  VideoPlayerVC.m
//  Cunli
//
//  Created by lilubin on 15/7/10.
//  Copyright (c) 2015年 lilubin. All rights reserved.
//

#import "VideoPlayerVC.h"
#import "IMYWebView.h"

//@interface VideoPlayerVC () <UIWebViewDelegate, UIScrollViewDelegate>
@interface VideoPlayerVC () <IMYWebViewDelegate>


@property (nonatomic, strong) IMYWebView *videoWeb;
@property (nonatomic, strong) MBProgressHUD *HUD;

@end

@implementation VideoPlayerVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createWebView];
    [self createBackBtn];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"视频详情";
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)createBackBtn
{
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(0, 0, 28, 28);
    [leftBtn setImage:[UIImage imageNamed:@"regback01"] forState:UIControlStateNormal];
    leftBtn.contentEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0);
    [leftBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftBarItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    self.navigationItem.leftBarButtonItem = leftBarItem;
}

- (void)backAction
{
    [_HUD hide:YES];
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)createWebView
{
    _videoWeb = [[IMYWebView alloc] initWithFrame:self.view.bounds];
    _videoWeb.delegate = self;
    [self.view addSubview:_videoWeb];
    
    if(_videoWeb.usingUIWebView)
    {
        //        self.title = @"ClickRefresh-UIWebView";
        NSLog(@"UIWebView");
    }
    else
    {
        //        self.title = @"ClickRefresh-WKWebView";
        NSLog(@"WKWebView");
    }
    
    
    [_videoWeb loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:_urlStr]]];
}
- (void)webViewDidStartLoad:(IMYWebView *)webView
{
//    _HUD = [Config createHUD];
    _HUD = [self createHUD];
}

- (void)webViewDidFinishLoad:(IMYWebView *)webView
{
    [_HUD hide:YES];
}

- (void)webView:(IMYWebView *)webView didFailLoadWithError:(NSError *)error
{
    [_HUD hide:YES];
}

/*
 - (void)createWebView
 {
 _videoWeb = [[UIWebView alloc] initWithFrame:self.view.bounds];
 _videoWeb.delegate = self;
 _videoWeb.scrollView.delegate = self;
 _videoWeb.scrollView.bounces = NO;
 _videoWeb.translatesAutoresizingMaskIntoConstraints = NO;
 [self.view addSubview:_videoWeb];
 NSURL *url = [NSURL URLWithString:_urlStr];
 NSURLRequest *request = [NSURLRequest requestWithURL:url];
 [_videoWeb loadRequest:request];
 _HUD = [Config createHUD];
 //    _HUD.userInteractionEnabled = NO;
 }
 
 - (void)webViewDidFinishLoad:(UIWebView *)webView
 {
 [_HUD hide:YES];
 }
 
 - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
 {
 //    if ([error code] == NSURLErrorDomain) {
 //        NSLog(@"NSURLErrorDomain");
 //    }
 
 [_HUD hide:YES];
 }
 */

- (MBProgressHUD *)createHUD
{
    MBProgressHUD *HUD = [[MBProgressHUD alloc] initWithView:self.view];
    HUD.detailsLabelFont = [UIFont boldSystemFontOfSize:16];
    [self.view addSubview:HUD];
    [HUD show:YES];
    return HUD;
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    if (_HUD != nil) {
        [_HUD hide:YES];
    }
}

@end
